﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Emoney.DataAccess;
using LiuYan.Base;
using LiuYan.Entity;


namespace LiuYan.Service
{
   public class FdService
    {

       /// <summary>
       /// 编辑数据
       /// </summary>
       /// <param name="entity"></param>
       /// <returns></returns>
        public int Edit(TB_Fd entity)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@id",SqlDbType.Int),
                new SqlParameter("@Name",SqlDbType.VarChar,50), 
                new SqlParameter("@Tell",SqlDbType.VarChar,50),
                new SqlParameter("@Person",SqlDbType.VarChar,50),
                new SqlParameter("@Img",SqlDbType.VarChar,50),
                 new SqlParameter("@Date",SqlDbType.VarChar,50), 
                  new SqlParameter("@Add",SqlDbType.VarChar,150) 
            };
            parameters[0].Value = entity.Id;
            parameters[1].Value = entity.Name;
            parameters[2].Value = entity.Tell;
            parameters[3].Value = entity.Person;
            parameters[4].Value = entity.Img;
            parameters[5].Value = entity.Date;
            parameters[6].Value = entity.Add;
          
            string sql = entity.Id == 0 ?
                " INSERT INTO TB_Fd(Name,Tell,Img,[Add],Person,del,Date)" +
                "            VALUES(@Name,@Tell,@Img,@Add,@Person,0,@Date) " :
                 "UPDATE TB_Fd SET Name=@Name,Tell=@Tell,Person=@Person,Img=@Img,[Add]=@Add WHERE Id=@id";
            return SqlHelper.ExecuteNonQuery(Connection.conStr, CommandType.Text, sql, parameters);
        }
       
       /// <summary>
       /// 获取列表
       /// </summary>
       /// <param name="pageIndex"></param>
       /// <param name="pageCount"></param>
       /// <param name="fdname"></param>
       /// <returns></returns>
        public DataSet GetList(int pageIndex,int pageCount,string fdname)
        {
            SqlParameter parameter = new SqlParameter("@fdname", SqlDbType.VarChar, 50);
            parameter.Value = fdname;
            string where = "";
            where = fdname == "" ? where : (where + "and Name like  N'%'+@fdname+'%'");
            string sql = "select count(*) as count from TB_Fd where del=0 "+where+" select top(10) * from dbo.TB_Fd where id not in(select top(0) " +
                         "Id from TB_Fd order by id desc ) and del=0 "+where+" order by id desc ";
            var set= SqlHelper.ExecuteDataset(Connection.conStr, CommandType.Text, sql, parameter);
           set.Tables[0].TableName = "count";
           set.Tables[1].TableName = "data";
           return set;
        }

       
    }
}
